package com.freesky.biz.activity.controller;

import cn.hutool.core.convert.Convert;
import com.freesky.biz.activity.domain.TCouponBill;
import com.freesky.biz.activity.domain.TStockMonitor;
import com.freesky.biz.activity.service.ICouponBillService;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import com.freesky.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/activity/coupon/bill")
public class CouponBillController extends BaseController {

    @Autowired
    private ICouponBillService couponBillService;

    /**
     * 微信核销明细
     */
    @PreAuthorize("@ss.hasPermi('activity:couponbill:list')")
    @GetMapping("/list")
    public TableDataInfo list(TCouponBill couponBill) {
        startPage();
        if(StringUtils.isNotEmpty(couponBill.getStockId())){
            String stockId = couponBill.getStockId().trim();
            stockId = Convert.toDBC(stockId);
            stockId = stockId.replaceAll("\r\n",",");
            stockId = stockId.replaceAll("\\n",",");
            couponBill.setStockId(stockId);
        }
        //System.out.println(couponBill.getStockId());
        List<TCouponBill> list = couponBillService.findList(couponBill);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('activity:couponbill:list')")
    @GetMapping("/summary")
    public AjaxResult summary(TCouponBill couponBill) {
        if(StringUtils.isNotEmpty(couponBill.getStockId())){
            String stockId = couponBill.getStockId().trim();
            stockId = Convert.toDBC(stockId);
            stockId = stockId.replaceAll("\r\n",",");
            stockId = stockId.replaceAll("\\n",",");
            couponBill.setStockId(stockId);
        }
        System.out.println(couponBill.getStockId());


        return AjaxResult.success(couponBillService.selectSummaryData(couponBill));
    }

    @Log(title = "核销明细", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('activity:couponbill:export')")
    @GetMapping("/export")
    public AjaxResult export(TCouponBill couponBill,
                             HttpServletResponse response) {
        if(StringUtils.isNotEmpty(couponBill.getStockId())){
            String stockId = couponBill.getStockId().trim();
            stockId = Convert.toDBC(stockId);
            stockId = stockId.replaceAll("\r\n",",");
            stockId = stockId.replaceAll("\\n",",");
            couponBill.setStockId(stockId);
        }
//        if(StringUtils.isNotEmpty(couponBill.getStockId())){
//            couponBill.setStockId(Convert.toDBC(couponBill.getStockId()));
//        }
        return couponBillService.exportFile(couponBill);
    }

    @Log(title = "核销明细汇总", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('activity:couponbill:export')")
    @GetMapping("/exportSummary")
    public AjaxResult exportSummary(TCouponBill couponBill,
                             HttpServletResponse response) {
//        if(StringUtils.isNotEmpty(couponBill.getStockId())){
//            couponBill.setStockId(Convert.toDBC(couponBill.getStockId()));
//        }
        if(StringUtils.isNotEmpty(couponBill.getStockId())){
            String stockId = couponBill.getStockId().trim();
            stockId = Convert.toDBC(stockId);
            stockId = stockId.replaceAll("\r\n",",");
            stockId = stockId.replaceAll("\\n",",");
            couponBill.setStockId(stockId);
        }
        return couponBillService.exportSummaryFile(couponBill);
    }

}
